package com.dodock.banglapapers.server.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListProthomalo implements DDNNewsListInterface {
	
	@Override
	public String GenerateNewsList(String categoryid) throws IOException {
		
		//return "[{\"id\":\"-13\", \"title\":\"আপগ্রেড চলছে, অনুগ্রহপূর্বক পরে আবার চেষ্টা করুন\"}]";
		
		
		if(categoryid.equals("top")){
			return generateTopListFromMainSiteHomePage();
		}
		else if(categoryid.equals("latest")){ //FIXED FOR NEW SITE
			return generateLatestListFromMainSiteHomePage();
		}
		//regular
		else if(categoryid.equals("national")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("national");
		}
		else if(categoryid.equals("international")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("international");
		}
		else if(categoryid.equals("economy")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("economy");
		}
		else if(categoryid.equals("sports")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("sports");
		}
		else if(categoryid.equals("opinion")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("opinion");
		}
		else if(categoryid.equals("life_style")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("life_style");
		}
		else if(categoryid.equals("technology")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("technology");
		}
		else if(categoryid.equals("art_and_literature")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("art_and_literature");
		}
		else if(categoryid.equals("entertainment")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("entertainment");
		}
		//features
		else if(categoryid.equals("naksha")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("naksha");
		}
		else if(categoryid.equals("roshalo_mainpage")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("roshalo_mainpage");
		}
		else if(categoryid.equals("holyday")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("holyday");
		}
		else if(categoryid.equals("adhuna")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("adhuna");
		}
		else if(categoryid.equals("shapno")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("shapno");
		}
		else if(categoryid.equals("ananda")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("ananda");
		}
		else if(categoryid.equals("anna_alo")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("anna_alo");
		}
		else if(categoryid.equals("durporobash")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("durporobash");
		}
		else if(categoryid.equals("shilpo_o_sahitto")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("shilpo_o_sahitto");
		}
		else if(categoryid.equals("gollachut")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("gollachut");
		}
		else if(categoryid.equals("projonmo_dot_com")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("projonmo_dot_com");
		}
		else if(categoryid.equals("bondhushava")){ //FIXED FOR NEW SITE
			return generateListFromJuly2013FeatureLayoutPage("bondhushava");
		}
		/*else if(categoryid.equals("sports")){
			return generateListFromMainSiteDefaultCategoryPage("15");
		}
		else if(categoryid.equals("entertainment")){
			return generateListFromMainSiteDefaultCategoryPage("420");
		}
		else if(categoryid.equals("business")){
			return generateListFromMainSiteDefaultCategoryPage("50");
		}
		else if(categoryid.equals("editorial")){
			return generateListFromMainSiteDefaultCategoryPage("91");
		}
		else if(categoryid.equals("lastpage")){
			return generateListFromMainSiteDefaultCategoryPage("6");
		}
		else if(categoryid.equals("firstpage")){
			return generateListFromMainSiteDefaultCategoryPage("2");
		}
		else if(categoryid.equals("national")){
			return generateListFromMainSiteDefaultCategoryPage("90");
		}
		else if(categoryid.equals("local")){
			return generateListFromMainSiteDefaultCategoryPage("51");
		}
		else if(categoryid.equals("capital")){
			return generateListFromMainSiteDefaultCategoryPage("56");
		}
		else if(categoryid.equals("world")){
			return generateListFromMainSiteDefaultCategoryPage("52");
		}
		else if(categoryid.equals("study")){
			return generateListFromMainSiteDefaultCategoryPage("53");
		}
		else if(categoryid.equals("technology")){
			return generateListFromMainSiteDefaultCategoryPage("54");
		}
		else if(categoryid.equals("onnoalo")){
			return generateListFromMainSiteDefaultCategoryPage("42");
		}
		else if(categoryid.equals("anondo")){
			return generateListFromMainSiteDefaultCategoryPage("12");
		}
		else if(categoryid.equals("noksha")){
			return generateListFromMainSiteDefaultCategoryPage("45");
		}
		else if(categoryid.equals("roshalo")){
			return generateListFromMainSiteDefaultCategoryPage("27");
		}
		else if(categoryid.equals("chutirdine")){
			return generateListFromMainSiteDefaultCategoryPage("46");
		}
		else if(categoryid.equals("erfan")){
			return erfan("46");
		}*/
		
		return "[]";
	}
	
	private String erfan(String nativeid) throws IOException{
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/detail/date/2012-08-01/news/278517").userAgent("Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1").get();
			return doc.html();
		}
		catch (Exception e) {
			return e.getMessage();
		}
		
		/*
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/section/date/2012-07-28/category/" + nativeid).userAgent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5 BAVM/1.0.0").get();
			
			<div class="threeCol mb20 white">
			Elements mastheads = doc.select("div[class=threeCol mb20 white]"); 
			
			if(mastheads == null || mastheads.size() == 0){
				mastheads = doc.select("div[class=c_block_one pb0]");//business
			}
			
			if(mastheads == null || mastheads.size() == 0){
				mastheads = doc.select("div[class=leftSection]");//sports
			}
			
			Element masthead = mastheads.first();
			Elements links = null;
			if(masthead != null){
				links = masthead.select("a[href]");
			}
			
			if(links == null){
				return "[]";
			}
			
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			
			for(Element a:links){
				String href = a.attr("href").trim();
				
				if(href.startsWith("/detail/")){
					String id = href.substring(href.lastIndexOf('/') + 1);
					if(list.contains(id) == false){
						String text = a.text();
						String image = null;
						
						if(text == null || text.trim().isEmpty()){
							if(a.select("img") != null && a.select("img").size() > 0){
								Element img = a.select("img").first(); 
								text = img.attr("alt");
								image = img.attr("src");
							}
						}
						
						if(text == null || text.trim().isEmpty()){
							Elements altelems = a.parent().nextElementSibling().select("a");
							if(altelems != null && altelems.size() > 0 ){
								Element alta = altelems.first(); 
								text = alta.text();
							}
						}
						
						JsonObject news = new JsonObject();
						news.addProperty("id", id);
						news.addProperty("title", text);
						
						if(image == null){
							Element parent = a.parent();
							if(parent != null){
								Element parentSibling = parent.nextElementSibling();
								if(parentSibling != null){
									Elements imageElements = parentSibling.select("img");
									if(imageElements != null){
										Element img = imageElements.first();
										if(img != null){
											news.addProperty("image", img.attr("src"));
										}
									}
										
								}
							}
						}
						else{
							news.addProperty("image", image);
						}
						
						
						array.add(news);
						list.add(id);
					}
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
			return "[]";
		}
		*/
	}

	
	private String generateListFromJuly2013FeatureLayoutPage(String nativeid){
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/" + nativeid).timeout(30000).get();
			
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			
			//get level 1 links
			Elements l1newsHolders = doc.select("div[class^=each_news]");
			for(Element l1news : l1newsHolders){
				Element htitle = l1news.select("h2[class=title]").first();
				Element a = htitle.select("a").first();
				String href = a.attr("href").trim();
				
				if(href.contains("/article/")){
					int first_index = href.indexOf("prothom-alo.com/") + 16;
					int last_index = href.lastIndexOf("/");
					String id = href.substring(first_index, last_index);
					if(list.contains(id) == false){
						String text = a.text();
						
						JsonObject news = new JsonObject();
						news.addProperty("id", id);
						news.addProperty("title", text);
						
						Elements dconts = l1news.select("div[class^=content]");
						if(dconts != null && dconts.size() > 0){
							Element dcont = dconts.get(0);
							Elements imgs = dcont.getElementsByTag("img");
							if(imgs != null && imgs.size() > 0){
								Element img = imgs.get(0);
								String imgsrc = img.attr("src");
								if(imgsrc.startsWith("//")){
									imgsrc = "http:" + imgsrc;
								}
								news.addProperty("image", imgsrc);
							}
						}
						
						array.add(news);
						list.add(id);
					}
				}
			}
			
			
			
			//get box links
			
			
			
			
			return array.toString();
		}
		catch (Exception e) {
			return "[]";
		}
	}
	
	private String generateListFromMainSiteDefaultCategoryPage(String nativeid) throws IOException{
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/section/date/0000-00-00/category/" + nativeid).get();
			//<div class="threeCol mb20 white">
			Elements mastheads = doc.select("div[class=threeCol mb20 white]"); 
			
			if(mastheads == null || mastheads.size() == 0){
				mastheads = doc.select("div[class=c_block_one pb0]");//business
			}
			
			if(mastheads == null || mastheads.size() == 0){
				mastheads = doc.select("div[class=leftSection]");//sports
			}
			
			Element masthead = mastheads.first();
			Elements links = null;
			if(masthead != null){
				links = masthead.select("a[href]");
			}
			
			if(links == null){
				return "[]";
			}
			
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			
			for(Element a:links){
				String href = a.attr("href").trim();
				
				if(href.startsWith("/detail/")){
					String id = href.substring(href.lastIndexOf('/') + 1);
					if(list.contains(id) == false){
						String text = a.text();
						String image = null;
						
						if(text == null || text.trim().isEmpty()){
							if(a.select("img") != null && a.select("img").size() > 0){
								Element img = a.select("img").first(); 
								text = img.attr("alt");
								image = img.attr("src");
							}
						}
						
						if(text == null || text.trim().isEmpty()){
							Elements altelems = a.parent().nextElementSibling().select("a");
							if(altelems != null && altelems.size() > 0 ){
								Element alta = altelems.first(); 
								text = alta.text();
							}
						}
						
						JsonObject news = new JsonObject();
						news.addProperty("id", id);
						news.addProperty("title", text);
						
						if(image == null){
							Element parent = a.parent();
							if(parent != null){
								Element parentSibling = parent.nextElementSibling();
								if(parentSibling != null){
									Elements imageElements = parentSibling.select("img");
									if(imageElements != null){
										Element img = imageElements.first();
										if(img != null){
											news.addProperty("image", img.attr("src"));
										}
									}
										
								}
							}
						}
						else{
							news.addProperty("image", image);
						}
						
						
						array.add(news);
						list.add(id);
					}
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
			return "[]";
		}
	}
	
	private String generateLatestListFromMainSiteHomePage(){
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/").timeout(30000).get();
			//
			Elements mastheads = doc.select("div[class=tabs_content]");
			Elements subtabs = mastheads.select("div[class=each_tab oh db]");
			Element masthead = subtabs.get(1);
			Elements lis = masthead.getElementsByTag("li");
			//Elements links = masthead.select("a[href]");
			
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			
			for(Element li:lis){
				Elements as = li.getElementsByTag("a");
				Element a = as.get(0);
				String href = a.attr("href").trim();
				
				if(href.contains("/article/")){
					int first_index = href.indexOf("prothom-alo.com/") + 16;
					int last_index = href.lastIndexOf("/");
					String id = href.substring(first_index, last_index);
					if(list.contains(id) == false){
						String text = a.text();
						
						JsonObject news = new JsonObject();
						news.addProperty("id", id);
						news.addProperty("title", text);
						
						array.add(news);
						list.add(id);
					}
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
			return "[]";
		}
	}
	
	
	private String generateTopListFromMainSiteHomePage(){
		try{
			Document doc = Jsoup.connect(Settings.WWW_PROTHOM_ALO_COM + "/").get();
			//
			Elements mastheads = doc.select("div[class=pr20]"); 
			
			Element masthead = mastheads.first();
			Elements links = masthead.select("a[href]");
			
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			
			for(Element a:links){
				String href = a.attr("href").trim();
				
				if(href.startsWith("/detail/") &&
						a.parent() != null &&
						a.parent().parent() != null &&
						a.parent().parent().className() != null &&
						a.parent().parent().className().startsWith("fL ")){
					
					String id = href.substring(href.lastIndexOf('/') + 1);
					if(list.contains(id) == false){
						String text = a.text();
						String image = null;
						
						if(text == null || text.trim().isEmpty()){
							if(a.select("img") != null && a.select("img").size() > 0){
								Element img = a.select("img").first(); 
								text = img.attr("alt");
								image = img.attr("src");
							}
						}
						
						JsonObject news = new JsonObject();
						news.addProperty("id", id);
						news.addProperty("title", text);
						
						if(image == null){
							Element parent = a.parent();
							if(parent != null){
								Element parentSibling = parent.nextElementSibling();
								if(parentSibling != null){
									Elements imageElements = parentSibling.select("img");
									if(imageElements != null){
										Element img = imageElements.first();
										if(img != null){
											news.addProperty("image", img.attr("src"));
										}
									}
										
								}
							}
						}
						else{
							news.addProperty("image", image);
						}
						
						
						array.add(news);
						list.add(id);
					}
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
			return "[]";
		}
		
	}
	
	/*
	private String generateListFromMobileSiteMainPage(String nativeid){
		return "";
	}
	
	private String generateListFromMobileSiteCategoryPage(String nativeid){
		return "";
	}
	*/
	
}
